package teacher.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import teacher.bean.Award;
import teacher.bean.Flow;
import teacher.bean.Paper;
import teacher.bean.Teacher;
import teacher.service.flow;
import teacher.service.paper;
import tools.StringDate;

public class Uppaper extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private Paper paper;
	private File fileCom;
	private String fileComFileName;
	private String uploadPath;
	private String FilePath;
	private String dates;
	
	String suc;
	
	public Paper getPaper() {
		return paper;
	}

	public void setPaper(Paper paper) {
		this.paper = paper;
	}

	public String getSuc() {
		return suc;
	}

	public void setSuc(String suc) {
		this.suc = suc;
	}

	public String getDates() {
		return dates;
	}

	public void setDates(String dates) {
		this.dates = dates;
	}

	public String getFileComFileName() {
		return fileComFileName;
	}

	public void setFileComFileName(String fileComFileName) {
		this.fileComFileName = fileComFileName;
	}

	public File getFileCom() {
		return fileCom;
	}

	public void setFileCom(File fileCom) {
		this.fileCom = fileCom;
	}


	public String execute() throws Exception {		
		
		if(paper.getMeetClass()==null){
			paper.setMeetClass(0);
		}
		if(paper.getMeetName()==null){
			paper.setMeetName("0");
		}
	    if(paper.getMeetPlace()==null){
	    	paper.setMeetPlace("0");
	    }
	    if(paper.getVolume()==null){
	    	paper.setVolume("0");
	    }
	    if(paper.getEsi()==null){
	    	paper.setEsi(0);
	    }
	    if(paper.getType()==null){
	    	paper.setType(0);
	    }
	    if(paper.getJclass()==null){
	    	paper.setJclass(0);
	    }
		
		if(paper==null){
			return ERROR;
		}
		paper.setState(0);
		HttpServletRequest request = ServletActionContext.getRequest();
		Teacher tea = (Teacher) request.getSession().getAttribute("teacher");
		int teaId = tea.getTeaId();
	
		paper.setTeaId(teaId);

		if (dates != null && dates.length() > 0) {
			paper.setDate(new StringDate().StringToDate(dates));
		} else {
			paper.setDate(new Date());
		}

		if (fileCom != null) {

			InputStream is = new FileInputStream(fileCom);

			uploadPath = ServletActionContext.getServletContext().getRealPath(
					"/upload");

			File dir = new File(uploadPath);

			// 如果这个目录不存在,则创建它
			if (!dir.exists()) {
				dir.mkdir();
			}
			FilePath = uploadPath + File.separator + this.getFileComFileName();

			File toFile = new File(uploadPath, this.getFileComFileName());

			OutputStream os = new FileOutputStream(toFile);

			byte[] buffer = new byte[4096];

			int length = 0;

			while ((length = is.read(buffer)) > 0) {
				os.write(buffer, 0, length);
			}
			System.out.println("上传文件名" + fileComFileName);
			System.out.println(FilePath);

			is.close();

			os.close();

			paper.setUpload(fileComFileName);
		}
		if (new paper().insertPaper(paper)) {
			this.setSuc("success");
			
//			流程管理部分
			Paper p=new Paper();
			System.out.println(this.getPaper().getName()+"aaaa");
			p=new flow().selectPaperByName(this.getPaper().getName().toString());
			System.out.println(p.getPapId());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Flow f=new Flow();
			f.setFruitId(p.getPapId());
			f.setFruitKind(1);
			f.setDirDo(2);
			f.setDirTime(df.format(new Date()));
			f.setSubDo(2);
			f.setSubTime(df.format(new Date()));
			f.setTeaDo(0);
			f.setTeaTime(df.format(new Date()));
//			流程管理部分尾
			if(new flow().insertFlow(f)){
				return SUCCESS;
			}else{
				return SUCCESS;
			}
		} else {
			return ERROR;
		}
	}

}
